<?php 
	Class Flight extends CI_Controller{
		public function __construct(){
			parent::__construct();
			$this->load->model('model_flight');
		}		
		
		public function index(){
			$provide='';
			if(isset($_GET['provide'])) $provide=$_GET['provide'];
			$name='';
			if(isset($_GET['name'])) $name=$_GET['name'];
			$data['data']=$this->model_flight->getallbyprovide($provide);
			
			$data['template']='flight/flight';
			$this->load->view('layout',$data);
		}
		
		public function edit(){
			if(isset($_GET['id'])){
				$data['detail']=$this->model_flight->getdata(intval($_GET['id']));
			}else $data['detail']=NULL;	
			
			$data['template']='flight/flight_edit';
			$this->load->view('layout',$data);
		}
		
		public function insert(){
			$data['codeofairline']=$_POST['codeofairline'];			
			$data['description']=$_POST['description'];	
			if($_FILES['fuavatar']['name'] != NULL){
				$config['upload_path'] = './uploads/flight';
				$config['allowed_types'] = '*';
				$config['max_size']	= '3000';
				$config['max_width']  = '2704';
				$config['max_height']  = '2700';
				$this->load->library('upload',$config);
				if(!$this->upload->do_upload("fuavatar")){
					header('Location: '.base_url().'admin/flight/index');
					return FALSE;
				}else{
					$dataimg = $this->upload->data();
					$data['avatar'] = $dataimg['file_name'];
				}
			}
			$data['destination']=$_POST['destination'];
			$data['stop']=$_POST['stop'];
			$data['fromplace']=$_POST['fromplace'];
			$data['timearrive']=$_POST['timearrive'];
			$data['timedeparture']=$_POST['timedeparture'];
			$data['weekday']=$_POST['weekday'];
			$data['airline']=$_POST['airline'];
			$data['duration']=$_POST['duration'];
			$data['type']=$_POST['type'];
			$data['status']=$_POST['status'];
			
			$this->model_flight->add($data);
			header('Location: '.base_url().'admin/flight/index');
		}
		
		public function update(){
			if(isset($_GET["id"])){
				$data['codeofairline']=$_POST['codeofairline'];			
	  			$data['description']=$_POST['description'];	
	  			if($_FILES['fuavatar']['name'] != NULL){
	  				$config['upload_path'] = './uploads/flight';
	  				$config['allowed_types'] = '*';
	  				$config['max_size']	= '3000';
	  				$config['max_width']  = '2704';
	  				$config['max_height']  = '2700';
	  				$this->load->library('upload',$config);
	  				if(!$this->upload->do_upload("fuavatar")){
	  					header('Location: '.base_url().'admin/flight/index');
	  					return FALSE;
	  				}else{
	  					$dataimg = $this->upload->data();
	  					$data['avatar'] = $dataimg['file_name'];
	  				}
	  			}
	  			$data['destination']=$_POST['destination'];
	  			$data['stop']=$_POST['stop'];
	  			$data['fromplace']=$_POST['fromplace'];
	  			$data['timearrive']=$_POST['timearrive'];
				$data['timedeparture']=$_POST['timedeparture'];
				$data['weekday']=$_POST['weekday'];
	  			$data['airline']=$_POST['airline'];
	  			$data['duration']=$_POST['duration'];
				$data['type']=$_POST['type'];
	  			$data['status']=$_POST['status'];
				
				$this->model_flight->update($data,intval($_GET['id']));
				header('Location: '.base_url().'admin/flight/index');
			}	
		}
		
		public function delete(){
			if(isset($_GET["id"])){
				$this->model_flight->del(intval($_GET['id']));	
				header('Location: '.base_url().'admin/flight/index');
			}	
		}
		
		public function changestatus(){			
			if(isset($_GET["id"])){		
				$data=$this->model_flight->getdata(intval($_GET["id"]));								
				if(count($data)>0){
					if(isset($_GET["status"])){
						$data['status']=intval($_GET['status']);
						$this->model_flight->update($data,intval($_GET["id"]));
						header('Location: '.base_url().'admin/flight/index');
					}
				}
			}
		}
	}
?>